package com.yxx.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yxx.annotation.OptLogger;
import com.yxx.entityModel.vo.Result.ResponseResult;
import com.yxx.entityModel.vo.query.MenuQuery;
import com.yxx.entityModel.vo.request.MenuRequest;
import com.yxx.entityModel.vo.response.MenuOptionResponse;
import com.yxx.entityModel.vo.response.MenuResponse;
import com.yxx.entityModel.vo.response.MenuTreeResponse;
import com.yxx.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.yxx.constant.OptTypeConstant.*;

/**
 * 菜单控制器
 * @author yuxiangxun
 * @date 2025/1/22
 * @apiNote
 */
@RestController
@Slf4j
@Api(tags = "菜单模块接口")
public class MenuController {
    @Resource
    private MenuService menuService;

    /**
     * 查看菜单列表
     * @param menuQuery
     * @return
     */
    @ApiOperation(value = "查看菜单列表")
    @SaCheckPermission("system:menu:list")
    @GetMapping("/admin/menu/list")
    public ResponseResult<List<MenuResponse>> listMenuVO(MenuQuery menuQuery){
        return ResponseResult.success(menuService.listMenuVO(menuQuery));
    }

    /**
     * 添加菜单
     * @param menuRequest
     * @return
     */
    @OptLogger(value = ADD)
    @ApiOperation(value = "添加菜单")
    @SaCheckPermission("system:menu:add")
    @PostMapping("/admin/menu/add")
    public ResponseResult<?> addMenu(@Validated @RequestBody MenuRequest menuRequest){
        menuService.addMenu(menuRequest);
        return ResponseResult.success();
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @OptLogger(value = DELETE)
    @ApiOperation(value = "删除菜单")
    @SaCheckPermission("system:menu:delete")
    @DeleteMapping("/admin/menu/delete/{menuId}")
    public ResponseResult<?> deleteMenu(@PathVariable("menuId") Integer menuId){
        menuService.deleteMenu(menuId);
        return ResponseResult.success();
    }

    /**
     * 修改菜单
     * @param menuRequest
     * @return
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "修改菜单")
    @SaCheckPermission("system:menu:update")
    @PutMapping("/admin/menu/update")
    public ResponseResult<?> updateMenu(@Validated @RequestBody MenuRequest menuRequest){
        menuService.updateMenu(menuRequest);
        return ResponseResult.success();
    }

    /**
     * 获取菜单下拉树
     * @return  菜单树
     */
    @ApiOperation(value = "获取菜单下拉树")
    @SaCheckPermission("system:role:list")
    @GetMapping("/admin/menu/getMenuTree")
    public ResponseResult<List<MenuTreeResponse>> menuListTree(){
        return ResponseResult.success(menuService.menuListTree());
    }

    /**
     * 获取菜单选项树
     * @return
     */
    @ApiOperation(value = "获取菜单选项树")
    @SaCheckPermission("system:menu:list")
    @GetMapping("/admin/menu/getMenuOptions")
    public ResponseResult<List<MenuOptionResponse>> MenuOptionList(){
        return ResponseResult.success(menuService.menuOptionList());
    }

    /**
     * 编辑菜单
     * @param menuId
     * @return
     */
    @ApiOperation(value = "编辑菜单")
    @SaCheckPermission("system:menu:edit")
    @GetMapping("/admin/menu/edit/{menuId}")
    public ResponseResult<MenuRequest> editMenu(@PathVariable("menuId") Integer menuId){
        return ResponseResult.success(menuService.editMenu(menuId));
    }

}
